/**
 * Written By: MinHo Kim (minho.kim.korea@gmail.com)
 * Copyright (C) 2011, MinHo Kim, Software Expert Program, KAIST University, Seoul, Korea.
 * This program is free software; you can redistribute it and/or modify it 
 * under the terms of the GNU General Public License as published 
 * by the Free Software Foundation; version 2 of the License.
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; 
 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
 * See the GNU General Public License for more details.
 */
package kaist.sep502.tsp.controller;

import kaist.sep502.tsp.algorithm.heuristic.GreedySearchHeuristic;
import kaist.sep502.tsp.algorithm.heuristic.Heuristic;
import kaist.sep502.tsp.model.CityPath;
import kaist.sep502.tsp.model.Problem;


/**
 * @author minho kim
 *
 */
public class AStarSearch extends SearchAlgorithm {

	/* (non-Javadoc)
	 * @see kaist.sep502.tsp.controller.SearchAlgorithm#search()
	 */
	@Override
	public Solution search() {
		solution.expandedNodeCount = 0;
		solution.startStopWatch();
		
		CityPath cityPath = new CityPath(Problem.getDepartureCityNo());
		setHeuristicAlgorithm(new GreedySearchHeuristic());
		Heuristic heuristic;
		
		for(int i=0; i<Problem.getCitySize()+1; i++){
//			if(cityPath.getIndex() == Problem.getCitySize()){
//				cityPath.add(Problem.getCity(Problem.getGoalCityNo()), 0.0);
//			} else {
//				double hx = getHx(cityPath);
//				int cityNo = getHeuristicCityNo(cityPath);
//				cityPath.add(Problem.getCity(cityNo), hx);
//			}
			
			heuristic = heuristicAlgorithm.getHx(cityPath);
			cityPath.set(heuristic.cityNo, heuristic.hx);
			
			if(Problem.checkGoal(cityPath)){
				solution.setGoalCityPath(cityPath);
				solution.setSuccess(true);
				solution.stopStopWatch();
				return solution;
			}
		}
		
		return solution;
	}
}
